Substrings¶
🎨 replace
¶
'I applied to UofU'.replace('UofU', 'BYU')
'I applied to BYU'
replace
let's you replace a substring with another string.
'A normal looking phrase'.replace(' ', '-')
'A-normal-looking-phrase'
By default, replace
replaces all occurrences of the substring with its substitute.
'I like cats and cats'.replace('cats', 'dogs', 1)
'I like dogs and cats'
You can provide a 3rd argument to replace
to control how many substitutions can be made.
text = 'I like to eat pizza'
new_text = text.replace('pizza', 'soup')
print(text)
print(new_text)
I like to eat pizza I like to eat soup
replace
(and any other string function that returns a string) returns a new string.
It doesn't modify the input string.
🎨 in
¶
'BYU' in 'I am a student at BYU.'
True
'BYU' in 'This room is full of monkeys!'
False
Use the in
operator to determine whether a substring is present in a string.
🖌 Custom Character Classes¶
def is_vowel(letter: str) -> bool:
return letter in 'AEIOUaeiou'
found = ''
for letter in 'The Aeneid is ancient Roman literature.':
if is_vowel(letter):
found += letter
print(found)
eAeeiiaieoaieaue
def is_vowel(letter):
return letter.lower() in 'aeiou'
found = ''
for letter in 'The Aeneid is ancient Roman literature.':
if is_vowel(letter):
found += letter
print(found)
eAeeiiaieoaieaue
🧑🏽🎨 BYU Enthusiasts¶
Capitalize every letter in an input string that is part of BYU
.
"be happy. yup." -> "Be happY. YUp."
def is_byu(letter: str) -> bool:
return letter in 'byu'
def byu(text: str) -> str:
"""
Capitalize every letter of `text` that is part of 'BYU'
>>> byu('booky')
'BookY'
"""
new = ''
for letter in text:
if is_byu(letter):
new += letter.upper()
else:
new += letter
return new
def byu(text):
for letter in 'byu':
text = text.replace(letter, letter.upper())
return text
print(byu('write "byu" in all-caps. By byu!'))
write "BYU" in all-caps. BY BYU!
print(byu('Any student, boy or girl, young or old, can be a yodeler.'))
AnY stUdent, BoY or girl, YoUng or old, can Be a Yodeler.
🖌 Early-return¶
def is_bracket(letter):
return letter in '{}[]<>'
def has_brackets(text):
for letter in text:
if is_bracket(letter):
return True
return False
has_brackets('Just some words')
False
has_brackets('A Python list prints like this: [1, 2, 3, 4]')
True
👨🏼🎨 Odd Numbers¶
Write a function that indicates whether a string has odd-numbered digits in it.
(i.e. it has a '1'
, '3'
, '5'
, '7'
, or '9'
in it)
def has_odds(text: str):
"""Return True if the text has odd-numbered digits"""
for symbol in text:
if symbol in '13579':
return True
return False
has_odds('I have 2 apples to share with 4 people.')
False
has_odds('I have 2 apples, and there are 34 people, but I will eat them all!')
True
👩🏼🎨 True Blue¶
Write a function that indicates whether a text has any of the following substrings in it:
- BYU
- blue
- cougar
def is_true_blue(text):
"""Returns True if `text` contains any of: 'BYU', 'blue', or 'cougar'"""
key_words = ['byu', 'blue', 'cougar']
for key_word in key_words:
if key_word in text.lower():
return True
return False
is_true_blue('My friend goes to UVU')
False
is_true_blue('I love BYU!')
True
is_true_blue('The sky is blue'), is_true_blue('Blue skies, shining on me')
(True, True)
is_true_blue('Don\'t touch the cougar')
True
🎨 in
and list
¶
numbers = [1, 5, 9]
print(4 in numbers)
print(5 in numbers)
False True
words = ['foo', 'bar', 'baz']
print('pizza' in words)
print('baz' in words)
False True
👩🏻🎨 You already said that...¶
Write a program that queries a user for a list of fruits.
If the user says a fruit that has already been given, say "You already said that."
The program should ignore casing (uppercase vs lowercase).
Once 10 fruits have been given, print "Way to go!" and end the program.
fruits.py
¶
Fruit: pear Fruit: apple Fruit: Pear You already said that. Fruit: kiwi Fruit: BANANA Fruit: pear You already said that. Fruit: APPLE You already said that. Fruit: Orange Fruit: lemon Fruit: lime Fruit: cherry Fruit: orange You already said that. Fruit: guava Fruit: plum Way to go!
Key Ideas¶
- substrings
replace
in
- Custom character classes using
in
- Early-return pattern
in
andlist